package com.noble.meether.chat.ui.agora.act.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.noble.meether.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.ui.user.paygoods.GoodsConstant;
import com.noble.glamour.ui.user.paygoods.GoodsListPanel;
import com.noble.glamour.ui.user.paygoods.GoodsPayTypePanel;
import com.noble.library.view.base.BaseDialogFragment;
import com.noble.meether.MeetUIShow;
import com.noble.meether.ui.diamond.bean.Diamond;
import com.noble.meether.ui.diamond.bean.DiamondList;

import java.util.ArrayList;

/**
 * 钻石底部弹框
 * Created by Su on 2017/8/22.
 */
public class GoodsDiamondBottomDlg extends BaseDialogFragment implements View.OnClickListener, RequestComplete {
    private GoodsListPanel goodsPanel; // 商品列表
    private GoodsPayTypePanel payTypePanel; // 支付方式

    private ArrayList<Diamond> diamondList;

    public GoodsDiamondBottomDlg() {
        settWindowAnimations(R.style.AnimDownInDownOutOverShoot);
        setGravity(Gravity.BOTTOM);
        setDialogSizeRatio(-2, -1);
        setCancelable(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.f1_bottom_diamond_dlg);
        initView();
        return getContentView();
    }

    private void initView() {
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_recharge).setOnClickListener(this);

        // 商品
        LinearLayout container = (LinearLayout) findViewById(R.id.goods_container);
        goodsPanel = new GoodsListPanel(getContext());
        container.addView(goodsPanel.getContentView());

        // 支付方式
        LinearLayout payContainer = (LinearLayout) findViewById(R.id.pay_type_container);
        payTypePanel = new GoodsPayTypePanel(getContext());
        payContainer.addView(payTypePanel.getContentView());

        ModuleMgr.getCommonMgr().reqDiamondList(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recharge:  // 充值
                Diamond diamond = diamondList.get(goodsPanel.getPosition());
                MeetUIShow.pay(payTypePanel.getPayType(), GoodsConstant.PAY_DIAMOND, diamond.getId(), "购买钻石" + diamond.getDoPay());
                dismiss();
                break;

            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqDiamondList) {
            if (!response.isOk()) return;

            DiamondList data = (DiamondList) response.getBaseData();
            diamondList = data.getDiamondList();
            if (diamondList.size() <= 0) return;
            goodsPanel.refresh(diamondList);
        }
    }
}